/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.vdialog.model.instance;

import java.net.URL;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.dialog.schema.view.SelectNodeTypePanel;
import offset.nodes.client.vdialog.view.template.SelectTemplatePanel;
import offset.nodes.client.dialog.newnode.model.AbstractNewNodeConfigurator;
import offset.nodes.client.dialog.newnode.model.NewNodeConfigurator;
import offset.nodes.client.dialog.newnode.model.ObjectType;
import offset.nodes.client.dialog.newnode.model.ServiceProcessor;
import offset.nodes.client.dialog.newnode.model.Step;
import offset.nodes.client.tree.view.DetailPanelTreeConfiguratorPanel;

/**
 *
 * @author Walter Lütgenau
 */
public class TemplateInstanceNewNodeConfigurator extends AbstractNewNodeConfigurator implements NewNodeConfigurator {

    @Override
    public ObjectType getObjectType() {
        SelectNodeTypePanel selectNodeTypePanel = new SelectNodeTypePanel(new ServerModel(getService()));
        SelectTemplatePanel selectTemplatePanel = new SelectTemplatePanel(new ServerModel(getService()), selectNodeTypePanel);
        return new ObjectType(getBundle(), "contentType.templateFolder.name", "contentType.templateFolder.description",
                new Step[]{
                    new Step(getBundle(), "templateFolder.step1.name", selectNodeTypePanel),
                    new Step(getBundle(), "templateFolder.step2.name", selectTemplatePanel),
                    new Step(getBundle(), "templateFolder.step3.name",
                            new DetailPanelTreeConfiguratorPanel(new NewTemplateInstanceConfiguration(new ServerModel(getService()), selectTemplatePanel, getPath()), true, false))}, new ServiceProcessor(getService(), getPath()) {

            public URL process() {
                try {
                    SetTemplateInstance.Request request = new SetTemplateInstance.Request();
                    DetailPanelTreeConfiguratorPanel templateInstancePanel = (DetailPanelTreeConfiguratorPanel) getObjectType().getSteps()[3].getPanel();
                    request.setTemplateInstancePath(getPath());
                    request.setConfiguration(templateInstancePanel.getText());

                    ServerModel model = new ServerModel(getService());
                    model.sendRequest(request);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return null;
            }
        });

    }
}
